// pages/main/mywangpu/mywangpu.js
var app= getApp();
import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
import user from "../../../class/Utils/User"
Page({
  data: {
    cate_list:[],
    cate_code:[],
    cate_form:'请选择',
    cate_index:-1,
    
    moban_list:[],
    moban_code:[],
    moban_form:'请选择',
    moban_index:0,
    moban_pic:'',

    guojia_list:[],
    guojia_code:[],
    guojia_form:'请选择',
    guojia_index:-1,



    sheng_list:[],
    sheng_code:[],
    sheng_form:'请选择',
    sheng_index:-1,

    shi_list:[],
    shi_code:[],
    shi_form:'请选择',
    shi_index:-1,

    info:{},
    category:{},
    regional:{},
    moban:{},
    regional_code: 0,  // 最终地区代码
    country_code: 0, // 国家代码

    temp_piclog:'',
    temp_picmy:'',
    piclog:'',
    picmy:'',
    data:{}, // z最终要提交的数据集合
    disabled:0,
    uid :wx.getStorageSync('user').userid,
    id: 0,
  },
  onLoad: function (options) {
    tips.loading("加载中")
    this.info()
  },
  info: function () {
    var that = this
    http.req("/Api/wp_detail",{uid:that.data.uid},function (res) {
      console.log(res)
      if(res.piclog){
        that.setData({piclog:res.piclog})
        if(!/http/.test(res.piclog)){
          res.piclog = "http://img.i.album.toocle.com/0-0-0-1/"+res.piclog
        }
      }
      if(res.picmy){
        that.setData({picmy:res.picmy})
        if(!/http/.test(res.picmy)){
          res.picmy = "http://img.i.album.toocle.com/0-0-0-1/"+res.picmy
        }
      }
      res.wx_style = res.wx_style ? res.wx_style : 1
      res.state = res.state?res.state:-1
      that.setData({
         info:res,
         state:res.state,
         id:res.id,
         category:res.category_list,
         regional:res.regional_list,
         moban:res.moban
      })
      console.log(that.data.category)
      var cate_list = []
      var cate_code = []
      for(var i=0; i<that.data.category.items.length;i++){
        cate_list.push(that.data.category.items[i].name)
        cate_code.push(that.data.category.items[i].code)
      }

      that.setData({
         cate_list:cate_list,
         cate_code:cate_code
      })
      if(that.data.info.category){
        var code = that.data.info.category.substring(0,2)
        for(var i=0; i<cate_code.length; i++){
          if(code == cate_code[i]){
            var cate_index = i
            break
          }
        }
        that.setData({
          cate_index:cate_index,
          cate_form:cate_list[cate_index]
        })
      }
      // 国家
      var guojia_list = []
      var guojia_code = []
      for(var i=0; i<that.data.regional.items.length;i++){
        guojia_list.push(that.data.regional.items[i].name)
        guojia_code.push(that.data.regional.items[i].code)
      }
      // console.log(guojia_list)
      // console.log(guojia_code)
      that.setData({
         guojia_list:guojia_list,
         guojia_code:guojia_code
      })
      /*一级选中*/
      if(that.data.info.regional){
        code = that.data.info.regional.substring(0,2)
        for(var i=0; i<guojia_code.length; i++){
          if(code == guojia_code[i]){
            var guojia_index = i
            break
          }
        }
        that.setData({
          guojia_index:guojia_index,
          guojia_form:guojia_list[guojia_index],
          country_code:guojia_code[guojia_index],
          regional_code:guojia_code[guojia_index]
        })
        //省  二级下拉
        var sheng_item = 'items'+code
        var sheng_list = []
        var sheng_code = []
        for(var i=0; i<that.data.regional[sheng_item].length;i++){
          sheng_list.push(that.data.regional[sheng_item][i].name)
          sheng_code.push(that.data.regional[sheng_item][i].code)
        }
        // console.log(sheng_list)
        // console.log(sheng_code)
        that.setData({
           sheng_list:sheng_list,
           sheng_code:sheng_code
        })
        /*二级选中*/
        if(that.data.info.regional.length >=4){
          code = that.data.info.regional.substring(0,4)
          for(var i=0; i<sheng_code.length; i++){
            if(code == sheng_code[i]){
              var sheng_index = i
              break
            }
          }
          that.setData({
            sheng_index:sheng_index,
            sheng_form:sheng_list[sheng_index],
            regional_code:sheng_code[sheng_index]
          })

          // 市 三级下拉
          var shi_item = 'items'+code
          var shi_list = []
          var shi_code = []
          for(var i=0; i<that.data.regional[shi_item].length;i++){
            shi_list.push(that.data.regional[shi_item][i].name)
            shi_code.push(that.data.regional[shi_item][i].code)
          }
          // console.log(shi_list)
          // console.log(shi_code)
          that.setData({
             shi_list:shi_list,
             shi_code:shi_code
          })
          /*三级选中*/
          if(that.data.info.regional.length >=6){
            code = that.data.info.regional.substring(0,6)
            for(var i=0; i<shi_code.length; i++){
              if(code == shi_code[i]){
                var shi_index = i
                break
              }
            }
            that.setData({
              shi_index:shi_index,
              shi_form:shi_list[shi_index],
              regional_code:shi_code[shi_index]
            })
          }
          /*三级选中结束*/
        }
        /*二级选中结束*/
      }
      /*一级选中结束*/

      /* 模板开始*/
      var moban_list = []
      var moban_code = []
      var moban_pic  = []
      for(var i=0; i<that.data.moban.length;i++){
        moban_list.push(that.data.moban[i].name)
        moban_code.push(that.data.moban[i].code)
        moban_pic.push(that.data.moban[i].pic)
      }

      that.setData({
         moban_list:moban_list,
         moban_code:moban_code,
         moban_pic :moban_pic
      })
      if(that.data.info.wx_style){
        var code = that.data.info.wx_style
        for(var i=0; i<moban_code.length; i++){
          if(code == moban_code[i]){
            var moban_index = i
            break
          }
        }
        that.setData({
          moban_index:moban_index,
          moban_form:moban_list[moban_index]
        })
      }
      /*模板结束*/
      tips.loaded()
    })
  },
  //选择行业
  selectcate: function (e) {
    //console.log(e.detail.value)
    var that = this
    that.setData({
        cate_index: e.detail.value,
        cate_form: that.data.cate_list[e.detail.value]
    })
  },
  selectguojia: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.guojia_index){
      
      var code = that.data.guojia_code[e.detail.value]
      var sheng_item = 'items'+code
      var sheng_list = []
      var sheng_code = []
      for(var i=0; i<that.data.regional[sheng_item].length;i++){
        sheng_list.push(that.data.regional[sheng_item][i].name)
        sheng_code.push(that.data.regional[sheng_item][i].code)
      }
      //console.log(sheng_list)
      //console.log(sheng_code)
      that.setData({
        guojia_index: e.detail.value,
        guojia_form: that.data.guojia_list[e.detail.value],

        sheng_list:sheng_list,
        sheng_code:sheng_code,
        sheng_form:'请选择',
        sheng_index:-1,

        shi_list:[],
        shi_code:[],
        shi_form:'请选择',
        shi_index:-1,

        //regional_code:code,
        country_code:code
      })
      //console.log(that.data.regional_code)
    }
  },
  selectsheng: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.sheng_index){
      var code = that.data.sheng_code[e.detail.value]
      var shi_item = 'items'+code
      var shi_list = []
      var shi_code = []
      for(var i=0; i<that.data.regional[shi_item].length;i++){
        shi_list.push(that.data.regional[shi_item][i].name)
        shi_code.push(that.data.regional[shi_item][i].code)
      }
      // console.log(shi_list)
      // console.log(shi_code)
      that.setData({
        sheng_index: e.detail.value,
        sheng_form: that.data.sheng_list[e.detail.value],

        shi_list:shi_list,
        shi_code:shi_code,
        shi_form:'请选择',
        shi_index:-1,
        regional_code:code
      })
      //console.log(that.data.regional_code)
    }
  },
  selectshi: function (e) {
    //console.log(e.detail.value)
    var that = this
    if(e.detail.value != that.data.shi_index){
      that.setData({
        shi_index: e.detail.value,
        shi_form: that.data.shi_list[e.detail.value],
        regional_code:that.data.shi_code[e.detail.value]
      })
      //console.log(that.data.regional_code)
    }
  },

  //选择模板
  selectmoban: function (e) {
    var that = this
    that.setData({
        moban_index: e.detail.value,
        moban_form: that.data.moban_list[e.detail.value]
    })
  },

  upimg: function (e) {
    console.log(e.currentTarget.dataset.type)
    var that = this
    wx.chooseImage({
      count :1,
      sizeType: ['original', 'compressed'],
      success: function (res) {
        //console.log(res)
        //console.log(wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64"))
        if(res.tempFiles[0].size>5*1000*1000){
          tips.error("图片大于5M")
        }else{
          wx.getImageInfo({
            src: res.tempFilePaths[0],
            success (res) {
              var key = 'temp_pic'+e.currentTarget.dataset.type
              var val = 'pic'+e.currentTarget.dataset.type
              that.setData({
                [key]: res.path,
                [val]: 'data:image/'+res.type+';base64,'+wx.getFileSystemManager().readFileSync(res.path, "base64")
              })
            }
          })
        }
        
      }
    })
  },
  formSubmit:function(e){
    //console.log(e)
    var that = this
    var name = e.detail.value.name
    if(!name){
        tips.error("请输入旺铺名称")
        return false
    }
    var short_name = e.detail.value.short_name
    if(!short_name){
        tips.error("请输入旺铺简称")
        return false
    }
    var contact = e.detail.value.contact
    if(!contact){
        tips.error("请输入 联系人")
        return false
    }
    if(that.data.cate_index == -1){
      tips.error("请选择主营行业")
      return false
    }
    if(that.data.country_code == 0){
      tips.error("请选择国家")
      return false
    }
    if(that.data.regional_code == 0){
      tips.error("请选择地区")
      return false
    }
    if(that.data.id){
      var method = 'change'
      var title = '更新旺铺'
    }else{
      var method = 'create'
      var title = '创建旺铺'
    }
    wx.showModal({
        title: title,
        content: '您确定要提交吗',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确认')
            var user = wx.getStorageSync('user')
            var data = {
                uid:user.userid,
                name:name,
                short_name:short_name,
                style_id:that.data.info.style_id,
                category:that.data.cate_code[that.data.cate_index],
                country:that.data.country_code,
                regional:that.data.regional_code,
                contact:contact,
                wx_style:that.data.moban_code[that.data.moban_index],
                address:e.detail.value.address,
                tel:e.detail.value.tel,
                fax:e.detail.value.fax,
                zip:e.detail.value.zip,
                email:e.detail.value.email,
                mobile:e.detail.value.mobile,
                website:e.detail.value.website,
                intro:e.detail.value.intro,
            }
            if(that.data.id){
              data.id = that.data.id
            }
            if(that.data.piclog){
              data.piclog = that.data.piclog
            }
            if(that.data.picmy){
              data.picmy = that.data.picmy
            }
            console.log(data)
            that.setData({disabled:1})
            tips.loading("提交中")
            http.req("/Api/wp_"+method,data,function (res) {
              console.log(res)
              if(/successful/.test(res.exp)){
                tips.ok(title+"成功")
                that.setData({state:0})
                if(res.id){
                  that.setData({id:res.id})
                }
                if(res.files.piclog){
                  that.setData({piclog:res.files.piclog})
                }
                if(res.files.picmy){
                  that.setData({picmy:res.files.picmy})
                }
              }else{
                tips.error(title+"失败")
              }
              that.setData({disabled:0})
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
    })
  },

})